////////////////////////////////////////////////////////////////////////////////

#ifndef _CLIENT_H_
#define _CLIENT_H_

////////////////////////////////////////////////////////////////////////////////

#include "common_types.h"
#include <ctime>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>

/** Forward declarations */
class ClientGUI;
class ClientEngine;
class ClientNetwork;
struct ClientPacket;
struct ServerPacket;
//namespace boost { namespace asio   { class io_service; }
//                  namespace system { class error_code; }
//                };
 

////////////////////////////////////////////////////////////////////////////////


/** This class represents game client
*   It manipulates GUI, engine, network and synchronizes them
*
*  @author   Roman Pasechnik
*  @since    Jun 23th, 2009
*  @updated  Jun 23th, 2009
*/
class Client
{
////////////////////////////////////////////////////////////////////////////////

public:

////////////////////////////////////////////////////////////////////////////////


    /** Constructor */
    Client();


    /** Destructor */
    ~Client();


////////////////////////////////////////////////////////////////////////////////


    /** Start game */
    void Start();


    /** Process mouse motion from GUI */
    void ProcessMouseMotion( unsigned short _x, unsigned short _y );


    /** Process event from GUI */
    void ProcessEvent( STM::Event _event );


    /** Receive packet from server */
    void Receive( const ServerPacket& _packet );


    /** Send events from client to server */
    void SendEvents();


    /** Set client ID */
    void SetID( char _id );


    /** Synchronize server and client time */
    void SyncronizeTime( clock_t _serverTime,  clock_t _clientTime );


////////////////////////////////////////////////////////////////////////////////

private:    

////////////////////////////////////////////////////////////////////////////////


    /** Send packet to server */
    void Send( const ClientPacket& _packet );

    typedef boost::asio::deadline_timer DeadLineTimer;
    typedef boost::shared_ptr<DeadLineTimer> DeadLineTimerPtr;

    /** Draw packet from server */
    void Draw( const ServerPacket& _packet,
               const boost::system::error_code& _error,
               DeadLineTimerPtr _deadTimer );


////////////////////////////////////////////////////////////////////////////////


    /** IO service object */
    boost::asio::io_service* m_io_service;


    /** Client engine */
    ClientEngine* m_Engine;


    /** Client network */
    ClientNetwork* m_Network;


    /** Client network */
    ClientGUI* m_GUI;


    /** Client ID */
    int m_ClientID;


    /** Server time */
    clock_t m_ServerTime;


    /** Client time */
    clock_t m_ClientTime;


////////////////////////////////////////////////////////////////////////////////
};


////////////////////////////////////////////////////////////////////////////////

#endif

////////////////////////////////////////////////////////////////////////////////

